package com.zch.market.controller;

import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zch.market.entity.Provider;
import com.zch.market.service.ProviderService;

@Controller
@RequestMapping("ProviderController")
public class ProviderController{
	@Autowired
	private ProviderService ps;
	
	@RequestMapping("getAllProvider")
	public String getAllProvider(String providerName,String providerDesc,
			String pageIndex,HttpServletRequest request, HttpServletResponse response){
		if(pageIndex==null||pageIndex==""){//等于''时默认为第一页
			pageIndex = "1";
		}
		System.out.println("pageIndex="+pageIndex);
		System.out.println("providerDesc="+providerDesc);
		System.out.println("providerName="+providerName);
		//获得查询到的所有数据
		List<Provider> list = ps.getProviderByPage(providerName, providerDesc, Integer.parseInt(pageIndex));
		System.out.println(list);
		//获得总页数
		int totalPage = ps.getTotalPage(providerName, providerDesc);
		//将所有参数存入request域
		request.setAttribute("providerName", providerName);
		request.setAttribute("providerDesc", providerDesc);
		request.setAttribute("i",(Integer.parseInt(pageIndex)-1)*5);
		request.setAttribute("list", list);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("totalCount", ps.getTotalCount(providerName, providerDesc));
		request.setAttribute("totalPage", totalPage);
		return "providerAdmin";		
	}

	@RequestMapping("addProvider")
	public void addProvider(Provider provider, HttpServletRequest request, 
			HttpServletResponse response) throws Exception{
		boolean flag = ps.addProvider(provider);
		PrintWriter out = response.getWriter();
		if(flag){
			//添加成功
			String a = URLEncoder.encode("添加成功！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllProvider';</script>");
		}else{
			//添加失败
			String a = URLEncoder.encode("添加失败！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllProvider';</script>");
		}
		
	}

	@RequestMapping("deleteProvider")
	public void deleteProvider(int id,HttpServletRequest request, HttpServletResponse response) throws Exception {
		boolean flag = ps.deleteProvider(id);
		PrintWriter out = response.getWriter();
		if(flag){
			//删除成功
			//解决弹框乱码问题
			String a = URLEncoder.encode("删除成功！", "utf-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllProvider';</script>");
		}else{
			//删除失败
			//解决弹框乱码问题
			String a = URLEncoder.encode("删除失败！", "utf-8");
			out.println("<script>alert(decodeURIComponent('"+a+"'));window.location.href='getAllProvider';</script>");
		}	
	}

	@RequestMapping("updateProvider")
	public void updateProvider(Provider provider, HttpServletRequest request, HttpServletResponse response) throws Exception {
		boolean flag = ps.updateProvider(provider);
		PrintWriter out = response.getWriter();
		if(flag){
			//更新成功
			String a = URLEncoder.encode("更新成功！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllProvider';</script>");
		}else{
			//更新失败
			String a = URLEncoder.encode("更新失败！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllProvider';</script>");
		}
	}
	
	@RequestMapping("getProvider")
	public String getProviderById(int id, HttpServletRequest request){
		Provider provider = ps.getProviderById(id);
		System.out.println(provider);
		request.setAttribute("provider", provider);
		return "providerUpdate";
	}

}
